package cn.tedu.service;

import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;

import java.sql.SQLException;

/**
 * @author waqwb
 */
public class UserService {
    private UserDao userDao = new UserDao();

    /**
     * 验证邮箱
     *
     * @param email 邮箱
     * @return 邮箱可用：true , 邮箱不可用：false
     */
    public boolean checkEmail(String email) {
        // 根据email查询数据库
        User user = null;
        try {
            user = userDao.getUserByEmail(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user == null ? true : false;
    }

    /**
     * 保存用户
     *
     * @param user 用户信息
     * @return 数据库执行影响的行数
     */
    public int saveUser(User user) {
        int row = 0;
        try {
            row = userDao.saveUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 登录方法
     *
     * @param email    邮箱
     * @param password 密码
     * @return 用户信息
     */
    public User login(String email, String password) {
        User user = null;
        try {
            user = userDao.getUserByEmail(email);
            if (user != null && !user.getPassword().equals(password)) {
                user = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
